package com.youxue.teachin.webplugin;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import com.Navigator;
import com.google.gson.Gson;
import com.hybrid.webview.WVWebView;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.common.vo.ToKenVO;
import com.youxue.teachin.user.userSystem.UserSystem;


/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-10 19:19
 * @version: 9.1.0
 */

public class JavaScriptObject {
    private static final String h5_cache = "ht_cache";
    private WVWebView mWVWebView;
    private JavaScriptListener mRefreshListener;

    public interface JavaScriptListener {
        void endRefreshing();

        void beginRefreshing();

        void updateMenu(String string);

        void setRefreshState(int state);
    }

    public JavaScriptObject(WVWebView webView, JavaScriptListener refreshListener) {
        mWVWebView = webView;
        mRefreshListener = refreshListener;
    }

    @JavascriptInterface
    public void log(String tag, String message) {
        android.util.Log.e("致伟:" + tag, this.getClass().getSimpleName() + " message =" +message);
    }

    /**
     * 在ActionBar的右边添加menu
     *
     * @param s
     */
    @JavascriptInterface
    public void addMenu(String s) {

        //s = "{\"rightMenu\":{\"id\":100,\"name\":\"100\"}}";
        //s = "{\"rightMenus\":[{\"icon\":\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u\\u003d3965705221,"
        //    + "2010595691\\u0026fm\\u003d27\\u0026gp\\u003d0.jpg\",\"id\":100,\"name\":\"100\"},{\"id\":100,"
        //    + "\"name\":\"测试用例\"},{\"id\":100,\"name\":\"测试用例2\"},{\"id\":100,\"name\":\"测试用例3\"}]}";

        if (mRefreshListener != null) {
            mRefreshListener.updateMenu(s);
        }
    }

    /**
     * 页面跳转
     *
     * @param uri 页面地址
     */
    @JavascriptInterface
    public void navigator(String uri) {
        new Navigator.Builder()
            .withUrl(uri)
            .build().open();
    }

    /**
     * 开始刷星
     */
    @JavascriptInterface
    public void startRefreshing() {
        if (mRefreshListener != null) {
            mRefreshListener.beginRefreshing();
        }
    }

    /**
     * 停止刷新
     */
    @JavascriptInterface
    public void endRefreshing() {
        if (mRefreshListener != null) {
            mRefreshListener.endRefreshing();
        }
    }

    /**
     * 设置web是否需要下拉刷新功能
     * @param state 1 需要下拉刷新，其他 关闭下拉刷新
     */
    @JavascriptInterface
    public void setRefreshState(int state) {
        if (mRefreshListener != null) {
            mRefreshListener.setRefreshState(state);
        }
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @JavascriptInterface
    public String getUserInfo() {
        if (UserSystem.getInstance().isLogin()) {
            Gson gson = new Gson();

            return gson.toJson(UserSystem.getInstance().getUserVO());
        } else {
            return "-1";
        }
    }

    /**
     * 获取tocken信息
     *
     * @return
     */
    @JavascriptInterface
    public String getToken() {
        if (UserSystem.getInstance().isLogin()) {
            Gson gson = new Gson();
            ToKenVO toKenVO = new ToKenVO();
            toKenVO.token = UserSystem.token;
            toKenVO.tokenId = UserSystem.tokenId;
            return gson.toJson(toKenVO);
        } else {
            return "-1";
        }
    }

    /**
     * 检查是否登录
     */
    @JavascriptInterface
    public void checkLogin() {
        if (!UserSystem.getInstance().isLogin()) {
            UserSystem.getInstance().login();
        }
    }

    /**
     * 播放视频
     *
     * @param id   视频id
     * @param name 视频名称
     * @param key  视频key
     * @param url  视频播放地址
     */
    @JavascriptInterface
    public void viewVideo(String jsonStr) {
        Gson gson = new Gson();
        Class<UrlVo> urlVoClass = UrlVo.class;
        UrlVo url = gson.fromJson(jsonStr, urlVoClass);
        if (url == null || TextUtils.isEmpty(url.ossUrl)) {
            return;
        }

        Uri uri = Uri.parse(url.ossUrl);
        //调用系统自带的播放器
        //Intent intent = new Intent(Intent.ACTION_VIEW);
        //intent.addCategory(Intent.CATEGORY_DEFAULT);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //intent.setDataAndType(uri, "video/*");
        //ContextUtils.getContext().startActivity(intent);
        new Navigator.Builder()
            .setPageName("video")
            .addParameter("url", url.ossUrl)
            .addParameter("title", url.name)
            .build().open();

    }

    /**
     * 预览pdf文件
     *
     * @param id
     * @param name
     * @param key
     * @param url
     */
    @JavascriptInterface
    public void viewPdf(String jsonStr) {
        Gson gson = new Gson();
        UrlVo url = gson.fromJson(jsonStr, UrlVo.class);
        if (url == null) {
            return;
        }
        new Navigator.Builder()
            .setPageName("view_pdf")
            .addParameter("title", url.name)
            .addParameter("url", url.ossUrl)
            .build().open();

        //Uri uri = Uri.parse(url.ossUrl);
        //Intent intent = new Intent(Intent.ACTION_VIEW);
        //intent.addCategory(Intent.CATEGORY_DEFAULT);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //intent.setDataAndType(uri, "application/pdf");
        //ContextUtils.getContext().startActivity(intent);
    }

    /**
     * 设置缓存
     *
     * @param key   缓存key
     * @param value 缓存value
     */
    @JavascriptInterface
    public void setCache(String key, String value) {
        PreferencesUtils.saveString(h5_cache, key, value);
    }

    /**
     * 获取缓存key
     *
     * @param key
     */
    @JavascriptInterface
    public String getCache(String key) {
        return PreferencesUtils.getString(h5_cache, key);
    }

    public static class UrlVo{
        public String id;
        public String name;
        public String key;
        public String ossUrl;
    }
}
